﻿using UnityEngine;
using System.Collections.Generic;
using LitJson;

public class GameManager : MonoBehaviour
{    
    public GameObject tankPrefab;
    protected Dictionary<string, Player> players = new Dictionary<string, Player>();

    public static System.Action<string> playerMove;
    public static System.Action<UserData> submitMsg;
    public static System.Action<User> playerLogout;
    Player localPlayer;
    
    void Awake()
    {
        LoginPanel.loginSuccess += OnLogin;
        playerMove += PlayerMove;
        submitMsg += SubmitMsg;
        playerLogout += PlayerLogout;
    }

    private void OnDestroy()
    {
        LoginPanel.loginSuccess -= OnLogin;
        playerMove -= PlayerMove;
        submitMsg -= SubmitMsg;
        playerLogout -= PlayerLogout;
    }

    void OnLogin(string json, bool islocal)
    {
        UserData user = JsonUtility.FromJson<UserData>(json);
        Player player = CreaterPlayer(user);

        //是自己的玩家
        if (islocal)
        {
            GameObject.FindObjectOfType<LocalController>().SetLocol(player);
            localPlayer = player;
        }
        else
        {
            //发送自己的位置给服务器
            localPlayer.userdata.position = localPlayer.transform.localPosition;
            string jsonstr = JsonUtility.ToJson(localPlayer.userdata);
            //user.pos = localPlayer.transform.position;//新玩家的位置，不是自己的位置
            NetworkManager.Instance.SendMessage(Protocol.PlayerPosition_CMD, jsonstr);
        }
    }
    void PlayerLogout(User user)
    {
        if (players.ContainsKey(user.username))
        {
            //删除玩家
            Player player = players[user.username];
            Destroy(player.gameObject);
            //删除字典的引用
            players.Remove(user.username);
        }
    }
    void SubmitMsg(UserData user)
    {
        if (players.ContainsKey(user.username))
        {
            players[user.username].transform.localPosition = user.position;
        }
        else
        {
            CreaterPlayer(user);
        }
    }
    void PlayerMove(string json)
    {
        Dictionary<string, string> dic = JsonMapper.ToObject<Dictionary<string, string>>(json);
        string username = dic["username"];
        Vector3 dir = JsonUtility.FromJson<Vector3>(dic["dir"]);
        Vector3 rot = JsonUtility.FromJson<Vector3>(dic["rot"]);
        //指定玩家移动
        players[username].Move(dir);
        players[username].Rot(rot);
    }
    Player CreaterPlayer(UserData user)
    {
        //创建游戏对象      
        GameObject scene = GameObject.FindGameObjectWithTag("scene");  
        GameObject go = Instantiate(tankPrefab);
        go.transform.SetParent(scene.transform);    
        go.transform.localPosition = user.position;     
        go.transform.GetChild(0).GetComponent<MeshRenderer>().materials[0].color = user.color;
        go.transform.GetChild(1).GetComponent<MeshRenderer>().materials[0].color = user.color;
        go.transform.GetChild(2).GetComponent<MeshRenderer>().materials[0].color = user.color;
        go.transform.GetChild(3).GetComponent<MeshRenderer>().materials[0].color = user.color;

        Player player = go.GetComponent<Player>();
        player.userdata = user;

        players.Add(user.username, player);
        return player;      
    }
}

